/**
 * eCommerce Application Sample for J2EE Training
 * @author ndg
 *
 */
package shell;

import ecom.entity.Account;
//import ecom.entitycopy.AccountBean;
import ecom.exception.EcomException;
import ecom.session.EcomAdminRemote;
import ecom.session.EcomCustomerRemote;
import java.io.PrintStream;
import java.util.List;
import java.util.StringTokenizer;

public class AccountCommandImpl implements ShellCommand {

    private ShellContext context = null;
    
        


    public AccountCommandImpl(ShellContext context) {
        this.context = context;
 

    }

    public String getName() {
        return "account";
    }

    public String getUsage() {
        return "account";
    }

    public String getShortDescription() {
        String description = "Account management\n";
        if (true ==  context.getVar("admin")) {
            description += "\t  : -add [iban] [accountOwner] [balance] ........................................create an account\n";
            description += "\t  : -remove [account IBAN]..........................................delete an account\n";
        }
        else {
            description += "\t  : -deposit [account IBAN] [amount]................................deposit cash on account ID\n";
        }
        description += "\t  : -id [account IBAN]..................................................display specified account details\n";
        return description;
    }

    public void execute(String cmdline, PrintStream out, PrintStream err) throws InsuffisantPrivilege {
        StringBuffer res = new StringBuffer();
        EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) context.getVar("customerBean");
        EcomAdminRemote ecomAdminBean = (EcomAdminRemote) context.getVar("adminBean");
        

        StringTokenizer st = new StringTokenizer(cmdline, " ");

        // Ignore cmd
        st.nextToken();

        
        
        // without arg : list all accounts
        if (st.countTokens() == 0) {
            if (true != context.getVar("admin")) {
                throw new InsuffisantPrivilege();
            }
            
            for(Account account:ecomAdminBean.listAccount())
                        System.out.println(account);
           
        } // With option
        else {
            String option = st.nextToken();

            // adding
            if (option.equals("-add")) {
                if (true != context.getVar("admin")) {
                    throw new InsuffisantPrivilege();
                }
                if (st.countTokens() == 3) {
                    try {
                        String iban;
                        String accountOwner;
                        double balance;
                        //String login, pass, surname, address, birthdate;

                        iban = st.nextToken();
                        accountOwner = st.nextToken();
                        balance = Double.valueOf(st.nextToken());
                        ecomAdminBean.createAccount(iban, accountOwner, balance);

                        //int id = 
                        //res.append(context.returnCode("Account Created with id #" + id, RESULT_ACTION.SUCCESS));
                    } catch (EcomException e) {
                        //res.append(context.returnCode("Error with account creation", RESULT_ACTION.FAILED));
                        err.println("Error with account creation");
                    }
                } else {
                    //res.append(context.returnCode(Shell.INCORRECT_ARGS_NUMBER, RESULT_ACTION.FAILED));
                    err.println(Shell.INCORRECT_ARGS_NUMBER);
                }
            } // removing
            else if (option.equals("-remove")) {
                if (true != context.getVar("admin")) {
                    throw new InsuffisantPrivilege();
                }
                if (st.countTokens() == 1) {
                    try {
                        int id = Integer.parseInt(st.nextToken());
                        ecomAdminBean.deleteAccount(id);
                        
                    } catch (Exception ex) {
                        //res.append(context.returnCode(ex.toString(), RESULT_ACTION.FAILED));
                        err.println("Error with account remove" + ex);
                    }
                } else {
                    err.println(Shell.INCORRECT_ARGS_NUMBER);
                }
            } else if (option.equals("-id")) {
                if (st.countTokens() == 1) {
                    try {
                        int id = Integer.parseInt(st.nextToken());
                        Account a = ecomAdminBean.getAccountById(id);
                        //return printAccount(ecomCustomerBean, currentCurrency, out, a, currentOutput);
                        System.out.println(a);
                    } catch (Exception ex) {
                        //res.append(context.returnCode(ex.toString(), RESULT_ACTION.FAILED));
                        err.println("Error with account detail");
                    }
                } else {
                    err.println(Shell.INCORRECT_ARGS_NUMBER);
                }
            } else if (option.equals("-deposit")) {
                if (st.countTokens() == 2) {
                    try {
                        String iban = st.nextToken();
                        double amount = Double.valueOf(st.nextToken());
                        ecomCustomerBean.deposit(iban, amount);
                        //res.append(_context.returnCode("DEPOSIT OK", RESULT_ACTION.SUCCESS));
                    } catch (Exception ex) {
                        //res.append(_context.returnCode(ex.toString(), RESULT_ACTION.FAILED));
                        err.println("Error with account detail");
                    }
                } else {
                   // res.append(context.returnCode(Shell.INCORRECT_ARGS_NUMBER, RESULT_ACTION.FAILED));
                    err.println(Shell.INCORRECT_ARGS_NUMBER);
                }
            } else {
                //res.append(context.returnCode(option + " is not known for Account Management", RESULT_ACTION.FAILED));
                err.println("\t" + option + " is not known for Account Management");
            }
        }
        out.println(res.toString());
    }
    }

